package com.yanqu.road.server.cmd.union.cross.parergon;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.Parergon;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = Protocol.S_UNION_PARERGON_BUY_PRODUCT_FROM_CROSS, desc = "购买")
public class BuyCmdFromCross extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        CrossUnionProto.ParergonBuyCrossRespMsg respMsg = CrossUnionProto.ParergonBuyCrossRespMsg.parseFrom(packet.getBytes());
        long price = respMsg.getPrice();

        if(price > 0){
            Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(price));
            player.getModule(PreReduceModule.class).restorePreReduce(property);
            player.getModule(CurrencyModule.class).removeCurrency(property,  eLogMoneyType.Union, eLogMoneyType.UnionDiscountShopBuy);
        }

        Property property = PropertyHelper.parseStringToProperty(respMsg.getReward());
        if (price < 0) {
            property.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(price * -1));
        }

        //加道具
        player.getModule(CurrencyModule.class).addCurrency(property,  eLogMoneyType.Union, eLogMoneyType.UnionDiscountShopBuy);

        Parergon.BuyProductRespMsg.Builder msg = Parergon.BuyProductRespMsg.newBuilder();
        msg.setReward(PropertyHelper.parsePropertyToString(property));
        msg.setRet(0);
        player.sendPacket(Protocol.U_UNION_PARERGON_BUY_PRODUCT, msg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
